package com.syqc.choose.car

import android.app.Activity
import android.view.View
import android.widget.ImageView
import androidx.core.view.isVisible
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import autodispose2.autoDispose
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.syqc.comlib.base.LazyFragment
import com.syqc.comlib.rx.filterFast
import com.syqc.comlib.utils.hideSoftInput
import com.syqc.comlib.utils.loadGif
import com.syqc.comlib.utils.loadStringDImgError
import com.syqc.comlib.utils.setResultTo
import com.syqc.comlib.utils.subPlate
import com.syqc.comlib.utils.toast
import com.syqc.entity.HistoryCars
import com.syqc.monitor.R
import com.syqc.monitor.databinding.FragmentHistoryCarBinding
import com.syqc.monitor.databinding.IncludeSelectSureBinding
import com.syqc.utils.Keys
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers

/**
 *@Author Jasper
 *@Time   2020/7/22
 *@Desc   历史车辆
 */
class HistoryFragment(
    private val isVideo: Boolean = false,
    private val isVideoCar: Boolean = false,
    private val isChange: Boolean = false
) :
    LazyFragment<FragmentHistoryCarBinding>(FragmentHistoryCarBinding::inflate) {
    private lateinit var mSureVB: IncludeSelectSureBinding

    private val mAdapter by lazy { HistoryCarAdapter() }
    private val mVM by viewModels<HistoryVM>()

    //空布局
    private val emptyView by lazy {
        val view = layoutInflater.inflate(
            R.layout.layout_empty_view,
            binding.rvHistory,
            false
        )
        view.findViewById<ImageView>(R.id.ivNoData).loadGif(requireContext(), R.mipmap.ic_empty)
        view
    }


    override fun initView(view: View) {
        binding.rvHistory.layoutManager = LinearLayoutManager(context)
        binding.rvHistory.adapter = mAdapter

        mAdapter.setOnItemClickListener { adapter, view, position ->
            hideSoftInput()
            mAdapter.index = position
            adapter.notifyDataSetChanged()
            mVM.carInfo = mAdapter.data[position]
        }

        mSureVB = IncludeSelectSureBinding.bind(binding.root)
        mVM.isVideoCar = isVideoCar
        mVM.isChange = isChange
        mVM.isVideo = isVideo

        ChooseCars.isVideo = isVideo
        mSureVB.tvSure.setText(R.string.desc_com_sure)

        mSureVB.tvSure.filterFast {
            if (mVM.carInfo == null) {
                requireContext().toast(R.string.hint_toast_choose_car)
                return@filterFast
            }
            mVM.chooseCar()
        }
        mVM.toastObserver().observeOn(AndroidSchedulers.mainThread())
            .autoDispose(scopeProvider).subscribe(::toastObserver)

        mVM.carLive.observe(this) {
            it.run {
                setResultTo(activity as Activity) {
                    putExtra(Keys.INTENT_CAR_ID, carId)
                    putExtra(Keys.INTENT_CAR_NAME, subPlate(carPlate, 14))
                    putExtra(Keys.INTENT_TEAM_NAME, teamName)
                    putExtra(Keys.INTENT_NO_AUTH, noAuth)
                    if (videos != null) putExtra(Keys.VIDEO, videos)
                }
                requireActivity().finish()
            }
        }

        mVM.carsLive.observe(this) {
            if (it.isNullOrEmpty()) {
                mAdapter.setEmptyView(emptyView)
                mSureVB.tvSure.isVisible = false
            } else {
                mSureVB.tvSure.isVisible = true
                mAdapter.setList(it)
            }
        }

    }

    override fun initData() {
        ChooseCars.initData()

    }

    override fun initEvent() {

    }

}

class HistoryCarAdapter :
    BaseQuickAdapter<HistoryCars.ListBean, BaseViewHolder>(R.layout.item_search_choose_car) {
    var index: Int = -1
    override fun convert(holder: BaseViewHolder, item: HistoryCars.ListBean) {
        item.run {

            val check = holder.getView<ImageView>(R.id.ivItemChoose)
            check.isSelected = index == holder.layoutPosition
            holder.getView<ImageView>(R.id.ivCarLogo).loadStringDImgError(context, iconLink)
            holder.setText(R.id.tvCarPlate, carPlate)
            holder.setText(R.id.tvCarName, carName)

        }
    }
}